package com.cxyjy.cfyuiapi.service.impl;

import com.cxyjy.cfyuiapi.service.VideoService;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @Author: jony
 * @Date : 2024/11/21 09:10
 * @VERSION v1.0
 */
@Service("VideoService")
public class VideoServiceImpl implements VideoService {
    private final RestTemplate restTemplate;



    public VideoServiceImpl(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @Override
    public ResponseEntity<byte[]> getVideo(String url) {
        try {
            // 发起HTTP GET请求
            ResponseEntity<byte[]> response = restTemplate.exchange(
                    url,
                    HttpMethod.GET,
                    null, // 请求体为空，因为我们是在获取数据
                    byte[].class // 响应体类型为byte[]
            );

            // 检查响应状态码
            if (response.getStatusCode() == HttpStatus.OK &&
                    response.getHeaders().getContentType().equals(MediaType.parseMediaType("video/mp4"))) {
                // 返回视频数据的ResponseEntity
                return response;
            } else {
                // 如果响应不是200 OK或者ContentType不正确，返回适当的错误响应
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
            }
        } catch (Exception e) {
            // 处理异常，例如网络连接问题、超时等
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }


}
